from collections import defaultdict
from collections import OrderedDict

def test1():
    # Test1: dict对于新key是否会自动增加子项
    dict1 = defaultdict(list)
    list1 = dict1["List1"]
    list1.append("a")
    print(dict1)
    print(list1)
    # Test1结论：用defaultdict参数指明子项的类型，会自动增加子项


def sort_value():
    # 声明字典
    key_value = {}

    # 初始化
    key_value["a"] = 56
    key_value["b"] = 2
    key_value["c"] = 12
    key_value["e"] = 24
    key_value["f"] = 18
    key_value["g"] = 323

    print("按值(value)排序:")
    sorted_set = sorted(key_value.items(), key=lambda kv:(kv[1], kv[0]), reverse=True)
    #l = len(sorted_set)
    #i = int(l/3)
    for key, value in sorted_set:
        print(key, value)


def sort_abs_value():
    # 声明字典
    key_value = {}

    # 初始化
    key_value["a"] = 56
    key_value["b"] = 2
    key_value["c"] = -12
    key_value["e"] = -24
    key_value["f"] = 18
    key_value["g"] = 323

    print("按值(value)排序:")
    sorted_set = sorted(key_value.items(), key=lambda kv: (abs(kv[1]), kv[0]), reverse=False)
    #l = len(sorted_set)
    #i = int(l/3)
    for key, value in sorted_set:
        print(key, value)


def sort_key():
    # 声明字典
    key_value = {}

    # 初始化
    key_value[2] = 56
    key_value[1] = 2
    key_value[5] = 12
    key_value[4] = 24
    key_value[6] = 18
    key_value[3] = 323

    print("按键(key)排序:")

    print(sorted(key_value)) # 返回重新排序的列表
    # 字典按键排序
    for i in sorted(key_value):
        print((i, key_value[i]), end=" ")


def delete_item():
    dict1 = {"a":1, "b":2, "c":3}
    print(dict1)
    del dict1["a"]
    print(dict1)
    del dict1["b"]
    del dict1["c"]
    print(dict1)
    #  结论：子项删光之后，字典还在，只是内容为空


def delete_in_loop():
    dict1 = {"a": 1, "b": 2, "c": 3}
    """
    # 错误
    for k in dict1.keys():
        del dict1[k]
    """
    # 正确
    for k in list(dict1.keys()):
        del dict1[k]
    print(dict1)

from abc import ABC
from dataclasses import dataclass

class A(object):

    aa = 1


def class_del_test():

    a = A()
    d1 = {"a": a}
    d2 = {"a": a}
    del d1["a"]
    print(d1)
    print(d2)
    print(d2["a"])


def class_key_test():
    a = A()
    d1 = {a:"a"}
    d2 = {a: "b"}
    print(d1)
    print(d2)


def dict_compare():
    dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    dict2 = {'a': 1, 'b': 2, 'c': 5, 'e': 6}

    differ = set(dict1.items()) ^ set(dict2.items())
    print(differ)
    # 所有差异
    # 输出:{('c', 3), ('e', 6), ('c', 5), ('d', 4)}
    diff = dict1.keys() & dict2
    print(diff)
    diff_vals = [(k, dict1[k], dict2[k]) for k in diff if dict1[k] != dict2[k]]
    print(diff_vals)
    # 相同key，不同value
    # 输出:[('c', 3, 5)]


def len_test():
    dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    print(len(dict1.keys()))


def dict_update():
    dict_1 = {
        "a":{
            "aa":1,
            "bb":2,
        },
        "b":{
            "aa":2,
            "bb":3
        }
    }
    dict_2 = {
        "b":{
            "aa":1,
            "bb":2,
        },
        "c":{
            "aa":2,
            "bb":3
        }
    }
    print(dict_1)
    dict_1.update(dict_2)
    print(dict_1)


def ordered_dict():
    dict1 = OrderedDict()
    dict1["b"] = "bb"
    dict1["a"] = "aa"
    for k, v in dict1.items():
        print(k, v)
    for k in dict1.__reversed__():
        print(k)

if __name__=="__main__":
    # test1()
    sort_value()
    #sort_abs_value()
    #sort_key()
    #delete_item()
    #class_del_test()
    #class_key_test()
    #dict_compare()
    #delete_in_loop()
    #len_test()
    #dict_update()
    #ordered_dict()
